/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.CooperBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.AdjustBillDao;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.CooperBillDao;
import com.minierp.dao.CooperBillItemDao;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.EstablishmentContactDao;
import com.minierp.dao.FinanceDetailDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.MakeTypeDao;
import com.minierp.dao.PayBillDao;
import com.minierp.dao.PayModeDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.NotAllowException;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncCooperBillFormCtrHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.Accessory;
import com.minierp.model.BusinessBill;
import com.minierp.model.CodeRule;
import com.minierp.model.CooperBill;
import com.minierp.model.CooperBillExt;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.Establishment;
import com.minierp.model.TallySet;
import com.minierp.service.CooperBillService;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class CooperBillFormCtr extends SimpleFormController {
	private BankAccountDao bankAccountDao;
	private DepartmentDao departmentDao;
	private CooperBillItemDao cooperBillItemDao;
	private CooperBillDao cooperBillDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private EstablishmentContactDao establishmentContactDao;
	private MakeTypeDao makeTypeDao;
	private PayBillDao payBillDao;
	private AdjustBillDao adjustBillDao;
	private BankAccountBalanceDao bankAccountBalanceDao;
	private BankAccountDetailDao bankAccountDetailDao;
	private PayModeDao payModeDao;
	private BusinessBillDao businessBillDao;
	private BusinessBillItemDao businessBillItemDao;
	private FinanceDetailDao financeDetailDao;
	private AccessoryDao accessoryDao;
	private IdGenerator idGenerator;
	private CooperBillService cooperBillService;
	private DataSource dataSource;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		String editType = request.getParameter("edit_type");
		refData.put("editType", editType);
		refData.put("departmentList", this.departmentDao.getList(true));
		List baList = this.bankAccountDao.getList("", "");
		EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		long dfTsId = 0L;
		long specId = 0L;
		if (id != null && !id.equals("")) {
			CooperBill bb = (CooperBill) this.cooperBillDao.get(
					CooperBill.class, Long.parseLong(id));
			specId = bb.getBankAccountId();
			dfTsId = bb.getTallySetId();
		} else {
			dfTsId = ub.getUser().getDfTsId();
		}

		refData.put("bankAccountList",
				PageHelper.parseBaList(baList, ent, ub, dfTsId, specId));
		refData.put("payModeList", this.payModeDao.getList(false));
		refData.put("makeTypeList", this.makeTypeDao.getList(false));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("COOPER_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for cooper bill");
		} else {
			CooperBillBean form;
			Establishment est;
			if (id != null && !id.equals("")) {
				CooperBill arg20 = (CooperBill) this.cooperBillDao.get(
						CooperBill.class, Long.parseLong(id));
				CooperBillExt arg21 = (CooperBillExt) this.cooperBillDao.get(
						CooperBillExt.class, Long.parseLong(id));
				if (arg20 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				est = (Establishment) this.cooperBillDao.get(
						Establishment.class, arg20.getCooperatorId());
				TallySet arg23 = (TallySet) this.cooperBillDao.get(
						TallySet.class, arg20.getTallySetId());
				List arg24 = this.cooperBillItemDao.getList(arg20.getId());
				form = EncCooperBillFormCtrHelper.doFormBackingObjectEdit(cr,
						arg20, arg21, est, arg24, arg23);
			} else {
				if (!EncCommonHelper.checkPrivilege(request, "COOP_BILL")) {
					throw new PermissionDennyException("Permission Denny");
				}

				String type = request.getParameter("type");
				String cooperatorId;
				List contacts;
				if (type != null && type.equals("BUSINESS")) {
					cooperatorId = request.getParameter("bb_id");
					BusinessBill arg22 = (BusinessBill) this.cooperBillDao.get(
							BusinessBill.class, Long.parseLong(cooperatorId));
					contacts = this.businessBillItemDao.getListWidthExt(arg22
							.getId());
					form = EncCooperBillFormCtrHelper.doFormBackingObjectNew(
							request, cr, arg22, contacts);
					if (!this.isFormSubmission(request)) {
						String tmpCode = form.getTmpCode();
						List accessories = this.accessoryDao.getList(
								"BUSINESS_BILL", arg22.getId(),
								new String[]{"COOPER"}, 0L);

						for (int i = 0; i < accessories.size(); ++i) {
							Accessory ta = (Accessory) accessories.get(i);
							String tFileName = ta.getFileName();
							String extendName = EncCommonHelper
									.getExtendName(tFileName);
							String uploadPath = EncCommonHelper
									.getUploadVirtualPath(this
											.getServletContext());
							String realPath = this.getServletContext()
									.getRealPath(uploadPath);
							String fileName = form.getCooperBill()
									.getCreaterId()
									+ "_"
									+ System.currentTimeMillis()
									+ "_"
									+ i
									+ "." + extendName;
							File tf = new File(this.getServletContext()
									.getRealPath("/")
									+ System.getProperty("file.separator")
									+ tFileName);
							if (tf.exists() && tf.isFile()) {
								FileCopyUtils.copy(tf, new File(realPath
										+ System.getProperty("file.separator")
										+ fileName));
								String newFileName = uploadPath + fileName;
								Accessory a = new Accessory();
								a.setAgentType("COOPER_BILL");
								a.setCreaterId(form.getCooperBill()
										.getCreaterId());
								a.setCreaterName(form.getCooperBill()
										.getCreaterName());
								a.setCreateTime(new Date());
								a.setFileName(newFileName);
								a.setId(this.idGenerator.getId(Accessory.class
										.getName()));
								a.setMark(ta.getMark());
								a.setCustomPath(ta.getCustomPath());
								a.setTmpCode(tmpCode);
								a.setSourceName(ta.getSourceName());
								this.accessoryDao.save(a);
							}
						}
					}
				} else {
					cooperatorId = request.getParameter("cooperator_id");
					est = (Establishment) this.cooperBillDao.get(
							Establishment.class, Long.parseLong(cooperatorId));
					contacts = null;
					if (est == null) {
						throw new Exception("cooperator not found for id:"
								+ cooperatorId);
					}

					contacts = this.establishmentContactDao
							.getList(est.getId());
					form = EncCooperBillFormCtrHelper.doFormBackingObjectNew(
							request, cr, est, contacts);
				}
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		CooperBillBean form = (CooperBillBean) command;
		EncCooperBillFormCtrHelper.doOnBind(request, form, this.dataSource);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String alertMsg = "";
		CooperBillBean form = (CooperBillBean) command;
		CooperBill obj = form.getCooperBill();
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
		String alertType;
		String flag;
		if (id != null && !id.equals("")) {
			alertType = request.getParameter("edit_type");
			if (alertType == null) {
				alertType = "";
			}

			if (alertType.equals("PRICE")) {
				if (!EncCommonHelper.checkPrivilege(request, "COOP_EDIT_PRIC")) {
					throw new Exception("Permission deny!");
				}
			} else if (alertType.equals("MODIFY")) {
				if (!EncCommonHelper.checkPrivilege(request, "COOP_MODI")) {
					throw new Exception("Permission deny!");
				}
			} else if (!EncCommonHelper.checkPrivilege(request, "COOP_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.cooperBillService.txUpdate(form, ent, alertType,
						this.cooperBillDao, this.cooperBillItemDao,
						this.payBillDao, this.adjustBillDao,
						this.bankAccountBalanceDao, this.bankAccountDetailDao,
						this.businessBillDao, this.businessBillItemDao,
						this.idGenerator, ub, this.financeDetailDao);
			} catch (CannotAcquireLockException arg13) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg14) {
				EncCommonHelper.setAlertMessage(request, arg14.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}

			alertMsg = "修改成功！";
		} else {
			alertType = request.getParameter("examine");
			if (alertType != null && alertType.equals("1")) {
				flag = "COOP_EXAM";
				if (!EncCommonHelper.checkPrivilege(request, flag)) {
					throw new Exception("Permission deny!");
				}
			}

			try {
				this.cooperBillService.txInsert(form, alertType, ent, ub,
						this.cooperBillDao, this.cooperBillItemDao,
						this.serialNumberDao, this.accessoryDao,
						this.bankAccountBalanceDao, this.businessBillDao,
						this.businessBillItemDao, this.idGenerator);
			} catch (CannotAcquireLockException arg15) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg16) {
				EncCommonHelper.setAlertMessage(request, arg16.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}

			alertMsg = "增加成功！";
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		alertType = request.getParameter("alert_type");
		flag = request.getParameter("continue_flag");
		if (flag != null && !flag.equals("")) {
			if (!flag.equals("CLOSE")) {
				if (flag.equals("PRINT")) {
					mv = new ModelAndView(new RedirectView(
							"/cooper/CooperBillPrint.ihtm?id=" + obj.getId()));
				} else if (flag.equals("NEW")) {
					mv = new ModelAndView(new RedirectView(
							"/cooper/CooperBillNew.ihtm?cooperator_id="
									+ obj.getCooperatorId() + "&alert_type="
									+ alertType));
				}
			}
		} else if (alertType == null || !alertType.equals("NONE")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			if (alertType != null && alertType.equals("FORM")) {
				mv = new ModelAndView(new RedirectView(
						"/cooper/CooperBillEdit.ihtm?id=" + obj.getId()
								+ "&alert_type=" + alertType));
			}
		}

		mv.addObject("alertType", alertType);
		return mv;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public BankAccountDao getBankAccountDao() {
		return this.bankAccountDao;
	}

	public void setBankAccountDao(BankAccountDao bankAccountDao) {
		this.bankAccountDao = bankAccountDao;
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public EstablishmentContactDao getEstablishmentContactDao() {
		return this.establishmentContactDao;
	}

	public void setEstablishmentContactDao(
			EstablishmentContactDao establishmentContactDao) {
		this.establishmentContactDao = establishmentContactDao;
	}

	public MakeTypeDao getMakeTypeDao() {
		return this.makeTypeDao;
	}

	public void setMakeTypeDao(MakeTypeDao makeTypeDao) {
		this.makeTypeDao = makeTypeDao;
	}

	public AdjustBillDao getAdjustBillDao() {
		return this.adjustBillDao;
	}

	public void setAdjustBillDao(AdjustBillDao adjustBillDao) {
		this.adjustBillDao = adjustBillDao;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public BankAccountBalanceDao getBankAccountBalanceDao() {
		return this.bankAccountBalanceDao;
	}

	public void setBankAccountBalanceDao(
			BankAccountBalanceDao bankAccountBalanceDao) {
		this.bankAccountBalanceDao = bankAccountBalanceDao;
	}

	public BankAccountDetailDao getBankAccountDetailDao() {
		return this.bankAccountDetailDao;
	}

	public void setBankAccountDetailDao(
			BankAccountDetailDao bankAccountDetailDao) {
		this.bankAccountDetailDao = bankAccountDetailDao;
	}

	public PayModeDao getPayModeDao() {
		return this.payModeDao;
	}

	public void setPayModeDao(PayModeDao payModeDao) {
		this.payModeDao = payModeDao;
	}

	public CooperBillDao getCooperBillDao() {
		return this.cooperBillDao;
	}

	public void setCooperBillDao(CooperBillDao cooperBillDao) {
		this.cooperBillDao = cooperBillDao;
	}

	public CooperBillItemDao getCooperBillItemDao() {
		return this.cooperBillItemDao;
	}

	public void setCooperBillItemDao(CooperBillItemDao cooperBillItemDao) {
		this.cooperBillItemDao = cooperBillItemDao;
	}

	public CooperBillService getCooperBillService() {
		return this.cooperBillService;
	}

	public void setCooperBillService(CooperBillService cooperBillService) {
		this.cooperBillService = cooperBillService;
	}

	public PayBillDao getPayBillDao() {
		return this.payBillDao;
	}

	public void setPayBillDao(PayBillDao payBillDao) {
		this.payBillDao = payBillDao;
	}

	public BusinessBillDao getBusinessBillDao() {
		return this.businessBillDao;
	}

	public void setBusinessBillDao(BusinessBillDao businessBillDao) {
		this.businessBillDao = businessBillDao;
	}

	public FinanceDetailDao getFinanceDetailDao() {
		return this.financeDetailDao;
	}

	public void setFinanceDetailDao(FinanceDetailDao financeDetailDao) {
		this.financeDetailDao = financeDetailDao;
	}

	public BusinessBillItemDao getBusinessBillItemDao() {
		return this.businessBillItemDao;
	}

	public void setBusinessBillItemDao(BusinessBillItemDao businessBillItemDao) {
		this.businessBillItemDao = businessBillItemDao;
	}

	public AccessoryDao getAccessoryDao() {
		return this.accessoryDao;
	}

	public void setAccessoryDao(AccessoryDao accessoryDao) {
		this.accessoryDao = accessoryDao;
	}
}